﻿using System;
using System.IO;
using System.Threading;
using BLL;
using Utils;

namespace UI.ModEmployee
{
    public partial class EmpInsuranceImport : System.Web.UI.Page
    {
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpInsuranceImport.aspx", RightChar.In))
            {
                if (!AccountBiz.CheckRight(account, "EmpInsuranceList.aspx", RightChar.In))
                {
                    JSUtility.Alert(this, "您没有导入权限！");
                    return;
                }
            }
            if (fUp.HasFile)
            {
               
                string strFileName = Server.HtmlEncode(fUp.FileName);
                string strExtension = Path.GetExtension(strFileName);
                if (strExtension != ".xls" && strExtension != ".xlsx")
                {
                    JSUtility.Alert(this, "请选择正确的Excel文件");
                    return;
                }
                string strUploadFileName = "~/log/Insurance"+ strExtension;
                if (System.IO.File.Exists(strUploadFileName)) System.IO.File.Delete(strUploadFileName);
                string servePath = Server.MapPath(strUploadFileName);

                try
                {
                    fUp.SaveAs(servePath);

                    Thread.Sleep(2000);


                    if (ExcelUtility.ImportInsurance(strExtension, servePath))
                        JSUtility.Alert(this, "导入正确");
                    else
                        JSUtility.Alert(this, "导入失败");
                }
                catch
                { JSUtility.Alert(this, "导入失败"); }
                finally
                {
                  //  if (File.Exists(servePath))
                      //  File.Delete(servePath);
                }

            }
        }
    }
}